package com.ext.aic.handler;

import android.content.Context;
import android.util.TypedValue;
import android.widget.TextView;

import com.ext.aic.R;
import com.ext.aic.beans.FlowItem;
import com.google.android.flexbox.FlexboxLayout;

import java.util.List;

public class FlowHandler {

    private TextView buildItemView(Context context, FlowItem flowItem) {
        TextView textView = new TextView(context);
        textView.setText(flowItem.getLabel());
        textView.setBackgroundResource(R.drawable.tag_background);
        textView.setTextColor(0xff3FB2FF);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
        textView.setPadding(20, 10, 20, 10);
        return textView;
    }

    private FlexboxLayout.LayoutParams buildItemLayoutParams(Context context) {
        FlexboxLayout.LayoutParams params = new FlexboxLayout.LayoutParams(
                FlexboxLayout.LayoutParams.WRAP_CONTENT,
                FlexboxLayout.LayoutParams.WRAP_CONTENT
        );
        params.setMargins(8, 8, 8, 8);
        return params;
    }

    public void addTagView(FlexboxLayout flexboxLayout, FlowItem flowItem) {
        Context context = flexboxLayout.getContext();
        TextView textView = buildItemView(context, flowItem);
        FlexboxLayout.LayoutParams params = buildItemLayoutParams(context);
        flexboxLayout.addView(textView, params);
    }

    public void bindTags(FlexboxLayout flexboxLayout, List<FlowItem> items) {
        flexboxLayout.removeAllViews();
        for (FlowItem flowItem : items) {
            addTagView(flexboxLayout, flowItem);
        }
    }
}
